<template>
	<view class="page">
		<!-- 搜索框start -->
		<view class="page-block">
			<view class="search-block">
				<view class="search-ico-wapper">
					<image class="serch-ico" src="http://www.imovietrailer.com/static/icos/search.png" mode=""></image>
					<input placeholder="找预告" maxlength="10" confirm-type="search" @confirm="searchMe" />
				</view>
			</view>
		</view>
		<!-- 搜索框end -->

		<!-- 电影片start -->
		<view class="movie-list">
			<view class="movie-wrapper" v-for="(item,index) in trailerList" :key='index'>
				<image class="poster" :src="item.cover" :data-trailerId="item.id" @click="showTrailer">
				</image>
			</view>
		</view>


		<!-- 电影片end -->
		<switch class="section-right" :checked="treatmentStatu.checked" @change="switchChange" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				trailerList: [],
				keywords: "", //搜索关键字
				page: 1, //当前第几页
				totalPages: 1, //总页数
				treatmentStatu: {
					checked: false
				},
			}
		},
		onLoad() {
			var me = this;

			uni.showLoading({
				mask: true, //防止客户在加载中的时候去点击页面
				title: '请稍后...'
			});
			uni.showNavigationBarLoading();
			uni.request({
				url: 'https://www.imovietrailer.com/superhero/search/list?qq=lee63875671&keywords=&page=&pageSize=',
				method: 'POST',
				success: (res) => {
					if (res.data.status == 200) {
						var trailerList = res.data.data.rows;
						me.trailerList = trailerList;
						console.log(me.trailerList)
					}
				},

				//下拉样式消失
				complete: () => {
					uni.hideNavigationBarLoading()
					uni.hideLoading() //隐藏loding
				}
			});

		},
		
		onReachBottom() {
			var me = this;
			var page = me.page + 1; //查询下一页面，当前页数累加1
			var keywords = me.keywords //获得当前页面data里面的搜索值
			var totalPages = me.totalPages //获取总页数 判断

			//如果当前需要分页数和总页数相等，就不分页
			if (page > totalPages) {
				return;
			}
			me.pagetrailerList(keywords, page, 15);

		},
		methods: {
			
			//开关
			switchChange(e) {
				let value = e.target.value
				let that = this
				this.$set(this.treatmentStatu, 'checked', value) // 将点击改变的状态赋给treatmentStatu.checked
				if (value && !this.havePromise) {
					uni.showModal({
						title: '提示',
						content: '您还没设置接诊承诺，是否前往设置',
						success: function(res) {
							if (res.confirm) {
								that.$emit('changePage', 1)
								console.log('用户点击确定')
							} else if (res.cancel) {
								that.$set(that.treatmentStatu, 'checked', false) // 手动修改switch的状态，视图会同步更新
								console.log('用户点击取消');
							}
						}
					});
				}
			},
			
			

			//跳转详情页面
			showTrailer(e) {
				var trailerId = e.currentTarget.dataset.trailerid;
				// console.log(e);
				// console.log(trailerId)
				//页面跳转接口api
				uni.navigateTo({
					url: "../movie/movie?trailerId=" + trailerId
				})


			},

			pagetrailerList(keywords, page, pageSize) {

				var me = this;

				uni.showLoading({
					mask: true, //防止客户在加载中的时候去点击页面
					title: '请稍后...'
				});
				uni.showNavigationBarLoading();
				uni.request({
					url: 'https://www.imovietrailer.com/superhero/search/list?qq=lee63875671&keywords=' + keywords +
						'&page=' + page +
						'&pageSize=' + pageSize,
					method: 'POST',
					success: (res) => {
						if (res.data.status == 200) {
							var templist = res.data.data.rows;
							me.trailerList = me.trailerList.concat(templist);
							me.totalPages = res.data.data.total; //获取总页数
							me.page = page; //覆盖当前页面里面的page
							console.log(me.trailerList)
						}
					},
					//下拉样式消失
					complete: () => {
						uni.hideNavigationBarLoading()
						uni.hideLoading() //隐藏loding
					}
				});

			},
			searchMe(e) {
				var me = this;
				//获取搜索内容
				var value = e.detail.value;
				console.log(value)
				me.keywords = value;
				me.trailerList = [];
				me.pagetrailerList(value, 1, 15)
			}

		}
	}
</script>

<style>
	@import url("search.css");
</style>
